package com.hmkj.web.controller.my;

import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.page.BasePageParams;
import com.hmkj.common.page.Pages;
import com.hmkj.core.mapper.user.UserMapper;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.user.UserService;
import com.hmkj.web.controller.GenericController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * ......................我佛慈悲......................
 * ......................_oo0oo_.......................
 * .....................o8888888o......................
 * .....................88" . "88......................
 * .....................(| -_- |)......................
 * .....................0\  =  /0......................
 * ...................___/`---'\___....................
 * ..................' \\|     |// '...................
 * ................/ \\|||  :  |||// \.................
 * .............../ _||||| -卍-|||||- \................
 * ..............|   | \\\  -  /// |   |...............
 * ..............| \_|  ''\---/''  |_/ |...............
 * ..............\  .-\__  '-'  ___/-. /...............
 * ............___'. .'  /--.--\  `. .'___.............
 * .........."" '<  `.___\_<|>_/___.' >' ""............
 * ........| | :  `- \`.;`\ _ /`;.`/ - ` : | |.........
 * ........\  \ `_.   \_ __\ /__ _/   .-` /  /.........
 * ....=====`-.____`.___ \_____/___.-`___.-'=====......
 * ......................`=---='.......................
 * ....................................................
 * .................佛祖开光 ,永无BUG...................
 */
@Api(description = "好友邀请相关")
@Slf4j
@RestController
@RequestMapping(GenericController.MEMBER_URL + "/invite")
public class MyInviteController extends GenericController<Integer, User> {
    @Resource
    private UserService userService;

    @ApiOperation(value = "个人中心-好友邀请-邀请记录", notes = "邀请记录")
    @ResponseBody
    @RequestMapping(value = "friends", method = RequestMethod.POST)
    public ResultEntity<Pages<User>> myRedPackageList(@ModelAttribute BasePageParams basePageParams) {
        User user = getLoginUser();
        Map<String, Object> param = new HashMap<>();
        param.put("basePageParams", basePageParams);
        param.put("parentId", user.getId());
        return ok(userService.selectByPage(basePageParams,
                q -> ((UserMapper) q).findCountForFriendsList(param)
                , q -> ((UserMapper) q).findListForFriendsList(param)));
    }

    @ApiOperation(value = "个人中心-好友邀请-登录用户邀请码", notes = "登录用户邀请码")
    @ResponseBody
    @RequestMapping(value = "invitecode", method = RequestMethod.POST)
    public ResultEntity<User> loginUser() {
        return ok(new User(user -> {
            user.setInviteCode(getLoginUser().getInviteCode());
        }));
    }

}
